home *** CD-ROM | disk | FTP | other *** search
/ MacHome 2001 June / MacHome Magazine Demo Disc June 2001.iso / Stuff / Software / Graphic / PageSpinner 3.0.2 / Examples / JavaScript / Random Link Example < prev    next >
Encoding:
Text File  |  2000-10-16  |  3.5 KB  |  136 lines  |  [TEXT/JyWs]

  1. <html><head>
  2. <title>JavaScript Random Link</title>
  3.  
  4. <script language="JavaScript" type="text/javascript">
  5. <!-- 
  6. /* 
  7.     GetRandomURL
  8.     Written by Jerry Aman, www.optima-system.com.
  9.     Part of the PageSpinner distribution.
  10.  
  11.     We will not be held responsible for any unwanted 
  12.     effects due to the usage of this script or any derivative.
  13.     No warrantees for usability for any specific application 
  14.     are given or implied.
  15.  
  16.     You are free to use and modify this script,
  17.     if credits are kept in the source code
  18. */
  19.  
  20.  
  21. function GetRandomURL()
  22. {
  23.     // Put relative or full URL's in the strings below
  24.     // You can increase the number of URL's to more than 5 by adding a
  25.     // string contaijavascript:GetRandomURL()ning an URL in list below
  26.  
  27. var locationlist = new URLList (
  28.     "harpo.html",
  29.     "groucho.html",
  30.     "chico.html",
  31.     "zeppo.html",
  32.     "Scrolling Text Stationery"
  33. );
  34.  
  35.     num = Math.round ( ( rand.next() * (locationlist.count-1)) );
  36.  
  37.     location.href = locationlist.list[num];
  38. }
  39.  
  40. function URLList ()
  41. {
  42.     var argv = URLList.arguments;
  43.     var argc = argv.length;
  44.     this.list = new Object();
  45.     for (var i = 0; i < argc; i++)
  46.     this.list[i] = argv[i];
  47.     this.count = argc;
  48.     return this;
  49. }
  50.  
  51. //*********************************************
  52. // Park-Miller Pseudo-Random Number Generator
  53. // JavaScript implementation by David N. Smith
  54. // of IBM's T J Watson Research Center
  55. //*********************************************
  56. function NextRandomNumber()
  57. {
  58.     var hi   = this.seed / this.Q;
  59.     var lo   = this.seed % this.Q;
  60.     var test = this.A * lo - this.R * hi;
  61.     if (test > 0)
  62.         this.seed = test;
  63.     else
  64.         this.seed = test + this.M;
  65.     return (this.seed * this.oneOverM);
  66. }
  67.  
  68. function RandomNumberGenerator() 
  69. {
  70.     var d = new Date();
  71.     this.seed = 2345678901 +
  72.     (d.getSeconds() * 0xFFFFFF) +
  73.     (d.getMinutes() * 0xFFFF);
  74.     this.A = 48271;
  75.     this.M = 2147483647;
  76.     this.Q = this.M / this.A;
  77.     this.R = this.M % this.A;
  78.     this.oneOverM = 1.0 / this.M;
  79.     this.next = NextRandomNumber;
  80.     return this;
  81. }
  82.  
  83. var rand = new RandomNumberGenerator();
  84.  
  85. // -->
  86. </script>
  87.  
  88. </head>
  89. <body bgcolor="#FFFFFF">
  90. <h1>JavaScript Random Link</h1>
  91.  
  92. <p><b>This stationery page contains a JavaScript that selects a random URL</b></p>
  93.  
  94. <p>
  95. The script is named <b>GetRandomURL</b> and it is placed in the HEAD section of the HTML document. The script is executed by clicking on a link containing a call to GetRandomURL(). Also note the custom text in the browser's status area when the cursor is over the link.</p>
  96.  
  97. <p>
  98. Example of this script picking a 
  99. <a href="javascript:GetRandomURL()" 
  100. onMouseOver="window.status='This link takes you to some unknown place'; 
  101. return true;">random</a> page.</p>
  102.  
  103. <blockquote>
  104.  
  105. <p>
  106. <b>How to use:</b></p>
  107.  
  108. <p>Replace the filenames inside the script with your own URLs and edit this page contents inside the <BODY> section. You can also copy the entire script to an existing page.</p>
  109.  
  110.  
  111. <pre>function GetRandomURL()
  112. {
  113. var locationlist = new URLList (
  114.     "harpo.html",
  115.     "groucho.html",
  116.     "chico.html",
  117.     "zeppo.html",
  118.     "http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/"
  119.     );
  120.  
  121. </pre>
  122.  
  123. <p>
  124. Use code similar to this to let the user execute the script:</p>
  125. <pre><A HREF="javascript:GetRandomURL()" 
  126. onMouseOver="window.status='This link takes you to some unknown place'; 
  127. return true;">random</A>
  128. </pre>
  129.  
  130. <p>
  131. Note that there seems to be an undocumented limit to the number of URLs that can be placed in the list, so if you have a large number of links, (more than 40 or 50), you may need to create multiple lists and extend the function GetRandomURL() to initially select a list at random.</p>
  132. </blockquote>
  133.  
  134. </body>
  135. </html>
  136.